

# remove everything
rm(list=ls())

# =================================================================================
# ---------------------------------------------------------------------
# Drop outliers in dataset "Data" according to the "v"th column
# at each percentage "p" at bottom and top. The default value of
# p is 0.005. v is a vector of numbers.
# ---------------------------------------------------------------------

outlier <- function(Data, v, p=0.005) {
  out <- c()
  for(i in v) {
    out <- rbind(out, quantile(Data[,i], probs=c(p, 1-p)))
  }
  j <- 1
  for(i in v) {
    Data <- Data[Data[,i]>out[j,1] & Data[,i]<out[j,2], ]; print(dim(Data))
    j <- j+1
  }
  return(Data)
}

# =================================================================================

# Load packages
library(foreign)
library(gmm)
library(plm)

Data <- read.dta('Ind39.dta')

Data <- outlier(Data, which(names(Data)=="sm"), p=0.005)

# Delete obs according to variable "Foreign"
Data <- subset(Data, F1>=0 & F1<=1 & !is.na(Province) & sm<=1 & !is.na(k2))

# Added the weighted k, l, and m. 
Data.t <- read.dta("Ind39_s_Dt_asym_Est.dta")
Data$wk1 <- Data.t$w1k1; Data$wl1 <- Data.t$w1l1; Data$wm1 <- Data.t$w1m1;

# Generate year dummies
Data$TD <- model.matrix(~as.factor(Data$Year)-1); Data$TD1 <- model.matrix(~as.factor(Data$Year-1)-1)[,-1]

# Calculate the length of loop
firm.list <- unique(Data$Firm)
R <- ceiling(length(firm.list)/50)

# empty obj.
JKpw <- JKpF <- JKpsw <- JKptil <- JKbeta <- JKcoef <- c()

for(ii in 1:R) {
  
  print(ii)
  # get jackknife samples
  from <- 50*(ii-1)+1; to <- 50*ii
  index <- Data$Firm %in% firm.list[from:to]
  D.jk <- Data[!index, ]
  
  # =============================================
  # Step One
  h.lnbe <- mean(log(D.jk$sm)); h.eta <- - log(D.jk$sm) + h.lnbe; h.e <- mean(exp(h.eta))
  h.betam <- exp(h.lnbe)/h.e
  
  # =============================================
  # Step Two
  # Define functions: "wphi" and "phi"
  wphi <- function(beta) {
    (1-h.betam)*D.jk$wm1 - h.lnbe - log(D.jk$ppi1/D.jk$ppii1) - beta[1]*D.jk$wk1 - beta[2]*D.jk$wl1 - D.jk$TD1%*%beta[3:9] 
  }
  
  phi <- function(beta) {
    (1-h.betam)*D.jk$m1 - h.lnbe - log(D.jk$ppi1/D.jk$ppii1) - beta[1]*D.jk$k1 - beta[2]*D.jk$l1 - D.jk$TD1%*%beta[3:9]
  }
  
  # -----------------------------------------------
  # Optimization
  objfn <- function(beta){
    y.star <- D.jk$y - h.betam*D.jk$m - beta[1]*D.jk$k - beta[2]*D.jk$l - D.jk$TD%*%beta[3:10]
    p1 <- as.vector(phi(beta)); p2 <- as.vector(wphi(beta)); 
    var.poly <- poly(cbind(p1, D.jk$F1, p2), degree = 2, raw = TRUE)
    mod1 <- lm(as.vector(y.star)~var.poly)
    OBJ <- sum(mod1$residuals^2)
    return(OBJ)
  }
  
  set.seed(1)
  initial <- c(0.05, 0.1, 0.05, 0,0,0,0,0,0,0,0)
  target <- optim(initial, objfn, method = "Nelder-Mead")
  
  # results - elas
  h.betak <- target$par[1]; h.betal <- target$par[2]
  
  G.coef <- function(beta){
    y.star <- D.jk$y - h.betam*D.jk$m - beta[1]*D.jk$k - beta[2]*D.jk$l - D.jk$TD%*%beta[3:10]
    p1 <- as.vector(phi(beta)); p2 <- as.vector(wphi(beta)); 
    var.poly <- poly(cbind(p1, D.jk$F1, p2), degree = 2, raw = TRUE)
    mod1 <- lm(as.vector(y.star)~var.poly)
    return(mod1$coef)
  }
  # results - partial effects
  coef <- G.coef(target$par)[2:10]
  
  # Define functions: "wphi" and "phi" for the full sample
  wphi <- function(beta) {
    (1-h.betam)*Data$wm1 - h.lnbe - log(Data$ppi1/Data$ppii1) - beta[1]*Data$wk1 - beta[2]*Data$wl1 - Data$TD1%*%beta[3:9] 
  }
  
  phi <- function(beta) {
    (1-h.betam)*Data$m1 - h.lnbe - log(Data$ppi1/Data$ppii1) - beta[1]*Data$k1 - beta[2]*Data$l1 - Data$TD1%*%beta[3:9]
  }
  
  w1 <- as.vector(phi(target$par)); sw1 <- as.vector(wphi(target$par))
  
  p.w  <- coef[1] + 2*coef[2]*w1 + coef[4]*Data$F1 + coef[7]*sw1
  p.F  <- coef[3] + coef[4]*w1 + 2*coef[5]*Data$F1 + coef[8]*sw1
  p.sw <- coef[6] + coef[7]*w1 + coef[8]*Data$F1 + 2*coef[9]*sw1
  
  # TIL
  D.b <- data.frame(Firm=Data$Firm, Year=Data$Year, Province=Data$Province, w2=Data.t$w2, pw=p.w, pF=p.F, psw=p.sw)
  D <- pdata.frame(D.b, index = c("Firm", "Year"))
  D$pF1 <- lag(D$pF, 1); 
  
  for(i in 1:nrow(D)) {
    s1.t <- (D$w2 > D$w2[i])*(D$Year==D$Year[i])*(D$Province==D$Province[i])*(!is.na(D$pF1)); s1.t[i] <- 0
    s1.b <- sum(s1.t)
    s1 <- if(s1.b>0) s1.t/s1.b else rep(0, length(s1.t));
    if(sum(s1)==0) D$sp[i] <- NA else D$sp[i] <- D$psw[i]*sum(s1*D$pF1, na.rm = TRUE)
  }
  
  p.til <- as.vector(D$sp)
  
  # Store bootstrap results
  JKpw <- cbind(JKpw, p.w); JKpF <- cbind(JKpF, p.F); JKpsw <- cbind(JKpsw, p.sw); JKptil <- cbind(JKptil, p.til)
  JKbeta <- cbind(JKbeta, c(h.betak, h.betal, h.betam))
  JKcoef <- cbind(JKcoef, coef)
  
}

save(JKpF, JKpw, JKpsw, JKptil, JKbeta, JKcoef, file = "Jack_s_Dt_asym.RData")
